﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using C1.Win.C1Ribbon;
using CWCS.BLL;


namespace CWCS.UI
{
    public partial class FrmTaskManager : C1RibbonForm
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        private int pageSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["pagesize"]);    //每页显示行数
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        private int nMax = 0;         //总记录数
        private int pageCount = 1;    //页数＝总记录数/每页显示行数
        private int pageCurrent = 1;   //当前页号
        private int nCurrent = 0;      //当前记录行
#pragma warning disable CS0414 // 字段“FrmTaskManager.gv_index”已被赋值，但从未使用过它的值
        private int gv_index = 0;
#pragma warning restore CS0414 // 字段“FrmTaskManager.gv_index”已被赋值，但从未使用过它的值
        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();
        TaskBLL taskbll = new TaskBLL();
        public FrmTaskManager()
        {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            BindData(1);
        }
        private void InitComBox()
        {
            DataTable dt = taskbll.GetZoneInfo();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["name"] = "--请选择--";
                dr["code"] = "";
                dt.Rows.InsertAt(dr, 0);
                cb_zoneid.DataSource = dt;
                cb_zoneid.DisplayMember = "name";
                cb_zoneid.ValueMember = "code";
            }
        }
        private void FrmTaskManager_Load(object sender, EventArgs e)
        {
            dtp_updatestart.Value = DateTime.ParseExact("00:00:00", "HH:mm:ss", null);
            dtp_updateend.Value = DateTime.ParseExact("23:59:59", "HH:mm:ss", null);
            InitComBox();
            BindData(1);
        }

        private void BindData(int load)
        {
            if (load == 1)
            {
                int tasktype, taskstatus;
                if (rb_glbtask.Checked)
                    tasktype = 0;
                else if (rb_sta.Checked)
                    tasktype = 1;
                else if (rb_sc.Checked)
                    tasktype = 2;

                else
                    tasktype = 2;
                if (rb_unfinish.Checked)
                    taskstatus = 0;
                else if (rb_excuting.Checked)
                    taskstatus = 2;
                else
                    taskstatus = 1;
                string zonecode ="";
                if(cb_zoneid.SelectedValue!=null)
                    zonecode = cb_zoneid.SelectedValue.ToString().Trim();
                dtInfo = taskbll.GetWCSTaskInfo(tasktype, taskstatus, dtp_updatestart.Value.ToString(), dtp_updateend.Value.ToString().Trim(), txt_wmstaskno.Text.Trim(), txt_taskno.Text.Trim(), txt_fromstation.Text.Trim(), txt_tostation.Text.Trim(), txt_barcode.Text.Trim(), string.IsNullOrEmpty(zonecode)?"":zonecode);

                if (dtInfo != null)
                {
                    nMax = dtInfo.Rows.Count;
                    pageCount = (nMax / pageSize);
                    if ((nMax % pageSize) > 0)
                    { pageCount++; }
                    pageCurrent = 1;
                    nCurrent = 0;
                }

            }
            if (dtInfo != null)
            {
                int nStartPos = 1;
                int nEndPos = 0;
                DataTable dtTemp = dtInfo.Clone();
                if (nMax > 0)
                {
                    if (pageCurrent == pageCount)
                    {
                        nEndPos = nMax;
                    }
                    else
                    {
                        nEndPos = pageSize * pageCurrent;
                    }
                    txtCurrentPage.Text = Convert.ToString(pageCurrent);
                }
                else
                {
                    nEndPos = 0;
                    txtCurrentPage.Text = "0";
                }
                nCurrent = pageSize * (pageCurrent - 1);
                nStartPos = nCurrent;
                lblPageCount.Text = "/ " + pageCount.ToString();
               
                //从元数据源复制记录行
                for (int i = nStartPos; i < nEndPos; i++)
                {
                    dtTemp.ImportRow(dtInfo.Rows[i]);
                    nCurrent++;
                }
                bindingNavigatorCountItem.Text = "/ " + nCurrent;
                bdsInfo.DataSource = dtTemp;
                bdnInfo.BindingSource = bdsInfo;
                gv_taskmanager.DataSource = bdsInfo;
                gv_taskmanager.Refresh();
                SortSerial();
            }
            else
            {
                gv_taskmanager.DataSource = dtInfo;
                SortSerial();
            }
        }

        private void gv_taskmanager_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (this.gv_taskmanager.RowSel > 0)
                {
                    c1ContextMenu1.ShowContextMenu(gv_taskmanager, new Point(e.X, e.Y));
                }
            }
        }

        private void c1CommandHolder1_CommandClick(object sender, C1.Win.C1Command.CommandClickEventArgs e)
        {
            if (e.Command.Name == "c1Command2")
            {
                if (gv_taskmanager.Rows.Selected.Count <= 0)
                    MessageBox.Show("请选择行");
                if (rb_unfinish.Checked || rb_excuting.Checked)
                {

                    if (rb_glbtask.Checked)
                    {
                        MessageBox.Show("请确认是否需要对主任务进行修改!", "警告！");
                    }
                    DataRow row = ((DataRowView)gv_taskmanager.Rows[gv_taskmanager.Row].DataSource).Row;
                    if (row == null)
                        return;
                    string fromstation = row["fromstation"].ToString().Trim();
                    string tasktype = row["tasktype"].ToString().Trim();
                    string barcode = row["barcode"].ToString().Trim();
                    string taskno = row["taskno"].ToString().Trim();
                    string taskstatus = row["taskstatus"].ToString().Trim();
                    string runstatus = row["runstatus"].ToString().Trim();
                    if (rb_glbtask.Checked)
                        tasktype = "GLB";
                    else if (rb_sta.Checked)
                        tasktype = "STA";
                    else if (rb_sc.Checked)
                        tasktype = "SC";

                    //if (!BLLBase.loginFlag)
                    //{
                    //    FrmLogIn frmLogIn = new FrmLogIn();
                    //    frmLogIn.ShowDialog();
                    //}
                    //if (!BLLBase.loginFlag)
                    //{
                    //    MessageBox.Show("用户校验失败,请重新校验!");
                    //    return;
                    //}
                    BLLBase.loginTime = DateTime.Now;

                    FrmTaskEdit formtaskedit = new FrmTaskEdit(fromstation, tasktype, taskno, barcode,
                    taskstatus, runstatus);
                    formtaskedit.ShowDialog();
                    if (FrmTaskEdit.ischange)
                    {
                        BindData(1);
                        FrmTaskEdit.ischange = false;
                    }

                }
            }
            else if (e.Command.Name == "c1Command1")
            {
                DataRow row = ((DataRowView)gv_taskmanager.Rows[gv_taskmanager.Row].DataSource).Row;
                Clipboard.SetDataObject(this.gv_taskmanager[gv_taskmanager.Row, gv_taskmanager.Col].ToString());
            }
            else if (e.Command.Name == "c1Command3")
            {
                if (gv_taskmanager.Rows.Selected.Count <= 0)
                    MessageBox.Show("请选择行");
                DataRow row = ((DataRowView)gv_taskmanager.Rows[gv_taskmanager.Row].DataSource).Row;
                if (row == null)
                    return;
                int lineblock = 3;
                int locationblock = 2;

                string toStation = string.Empty;
                string errormsg = string.Empty;
                string tasktype = row["tasktype"].ToString().Trim();
                string scno = row["equipmentnumber"].ToString().Trim();
                string barcode = row["barcode"].ToString().Trim();
                string oldlocationumber = row["tostation"].ToString().Trim();
                if (MessageBox.Show("确定要重新分配托盘" + barcode + "的入库货位吗?", "更换", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (oldlocationumber.Substring(0, 1) == "B" || oldlocationumber.Substring(0, 1) == "C" || oldlocationumber.Substring(0, 1) == "E")
                    {
                        lineblock = 1;
                    }
                    if (tasktype != "入库任务")
                    {
                        MessageBox.Show("只允许更换入库任务的地址!");
                    }
                    else
                    {
                        if (taskbll.WCSChanageLocInfo(scno, barcode, oldlocationumber, lineblock, locationblock, ref toStation, ref errormsg))
                        {
                            MessageBox.Show("将托盘:" + barcode + "的入库货位重新分配为:" + toStation);
                        }
                        else
                            MessageBox.Show("托盘:" + barcode + "重新分配入库货位失败,失败原因为:" + errormsg);
                    }
                }
            }
        }

        private void rb_glbtask_CheckedChanged(object sender, EventArgs e)
        {
            BindData(1);
        }

        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                if (pageCount > 1)
                {
                    if (e.ClickedItem.Text == "上一页")
                    {
                        pageCurrent--;
                        if (pageCurrent <= 0)
                        {
                            pageCurrent++;
                            return;
                        }
                        else
                        {
                           
                            BindData(0);
                        }
                    }
                    if (e.ClickedItem.Text == "下一页")
                    {
                        pageCurrent++;
                        if (pageCurrent > pageCount)
                        {
                            pageCurrent--;
                            return;
                        }
                        else
                        {
                            BindData(0);
                        }
                    }
                    if (e.ClickedItem.Text == "首页")
                    {
                        pageCurrent = 1;
                       
                        BindData(0);
                    }
                    if (e.ClickedItem.Text == "末页")
                    {
                        pageCurrent = pageCount;
                        BindData(0);
                    }
                }
                if (e.ClickedItem.Text == "刷新")
                {
                    BindData(1);
                }
            }
            catch { }
        }

        public void SortSerial()
        {
            try
            {
                for (int i = 1; i < gv_taskmanager.Rows.Count; i++)
                {
                    gv_taskmanager.Rows[i]["Serial"] = i;
                    
                }
            }
            catch
            {
            }
        }

    }
}
